package com.fenghongzhang.day009viewgroup;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

public class MyViewGroup extends FrameLayout {

    private static final String TAG = "MyViewGroup";

    TextView textView;
    LinearLayout linearLayout;
    LayoutParams layoutParams;

    View childAt;
    PointF pointF = new PointF();
    int height = 200;

    public MyViewGroup(Context context) {
        super(context);
        init();
    }

    public MyViewGroup(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        //布局
        linearLayout = new LinearLayout(getContext());
        //布局的参数
        layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);

        //隐藏
        layoutParams.topMargin = -height;
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setBackgroundColor(Color.RED);


        addView(linearLayout);

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        //终止事件派发
        return super.dispatchTouchEvent(ev);
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            childAt = getChildAt(i);
            if(i == 1){
                childAt.layout(getWidth()/2-100 ,height/2,getWidth()/2+200,height/2+50);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                pointF.x = ev.getX();
                pointF.y = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:

//                float y = Math.abs(ev.getY() - pointF.y);

                //取到布局属性
//                layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
                layoutParams = (LayoutParams) linearLayout.getLayoutParams();

                layoutParams.topMargin += ev.getY() - pointF.y;

                if(layoutParams.topMargin < -height){
                    layoutParams.topMargin = -height;
                }

                if(layoutParams.topMargin > 0){
                    layoutParams.topMargin = 0;
                }
                linearLayout.setLayoutParams(layoutParams);

                pointF.y = ev.getY();
                pointF.x = ev.getX();

                break;
            case MotionEvent.ACTION_UP:
                layoutParams = (LayoutParams) linearLayout.getLayoutParams();
                Log.i(TAG, "onTouchEvent: "+layoutParams.topMargin);
                if(layoutParams.topMargin > -height/2){
                    //展开
                    startAnim(layoutParams.topMargin,0);
                    childAt.setVisibility(VISIBLE);
                }else{
                    startAnim(layoutParams.topMargin,-height);
                    childAt.setVisibility(GONE);
                }

                break;

        }
        return true;
    }

    private void startAnim(int start , int end) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer animatedValue = (Integer) animation.getAnimatedValue();
                layoutParams = (LayoutParams) linearLayout.getLayoutParams();
                layoutParams.topMargin = animatedValue;
                linearLayout.setLayoutParams(layoutParams);
            }
        });
        valueAnimator.start();
    }


    //    @Override
//    public boolean onTouchEvent(MotionEvent event) {

//        return super.onTouchEvent(event);
//    }
}
